<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018-07-05
 * Time: 18:03
 */
// 开发允许跨域访问
header('Access-Control-Allow-Origin:*');
class Patient extends MY_Controller
{
    // 免费图文患者所剩追问次数
    public function inquiryNum(){
        $co_id = $this->input->post('co_id');
        $p_id = $this->input->post('p_id');

        if (empty($co_id) || empty($p_id)){
            $res['start'] = 300;
            $res['content'] = '请完整填写请求内容';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return;
        }

        $sql = " select inquiry_num as 'count' from conditions where id = ? ";

        $queryRes = $this->db->query($sql, array($co_id));
        $res['start'] = 200;
        $res['content'] = '所剩免费咨询次数';
        $res['data'] = $queryRes->row_array()['count'];
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
        return $res;
    }

    public function inquiry(){
        $co_id = $this->input->post('co_id');
        $p_id = $this->input->post('p_id');
        $inquiry = $this->input->post('inquiry');

        if (empty($co_id) || empty($p_id) || empty($inquiry)){
            $res['start'] = 300;
            $res['content'] = '请完整填写请求内容';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return;
        }

        $sql = " select inquiry_num as 'count' from conditions where id = ? ";
        $queryRes = $this->db->query($sql, array($co_id));
        $count = $queryRes->row_array()['count'];
        if ($count < 1){
            $res['start'] = 300;
            $res['content'] = '免费咨询已用完';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return ;
        }


        $co_do['co_id'] = $co_id;
        $co_do['p_id'] = $p_id;
        $co_do['inquiry'] = $inquiry;
        $co_do['createtime'] = date('Y-m-d h:i:sa',time());

        // 保存记录
        $this->db->insert('co_do', $co_do);

        // 追问次数减一
        $sql = " update conditions set inquiry_num = (inquiry_num - 1) where id = ? ";
        $this->db->query($sql, array($co_id));

        $res['start'] = 200;
        $res['content'] = '免费咨询成功';
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
    }

    // 患者评价
    public function feedback(){

        $co_id = $this->input->post('co_id');
        $star = $this->input->post('star');
        $feedback = $this->input->post('feedback');

        if (empty($co_id) || empty($star) || empty($feedback)){
            $res['start'] = 300;
            $res['content'] = '请完整填写请求内容';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return;
        }

        $sql = " update conditions set star = ?, feedback = ? where id = ? ";
        $this->db->query($sql, array($star, $feedback, $co_id));

        $res['start'] = 200;
        $res['content'] = '评价成功';
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
    }


}